package com.oriental.manage.service.specially.Impl;

import com.oriental.check.commons.util.BeanMapperUtil;
import com.oriental.manage.dao.specially.MerchantRiskInfoReportMapper;
import com.oriental.manage.model.specially.AreaNumber;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.pojo.specially.MerchantRiskInfo;
import com.oriental.manage.service.specially.BatchMessageProcess;
import com.oriental.riskfront.api.enums.MessageStatus;
import com.oriental.riskfront.api.model.specially.MerchantRiskInfoUpdateDto;
import com.oriental.riskfront.api.service.DataDictInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxin on 2017/4/25.
 */
@Service
@Slf4j
public class MerchantRiskInfoUpdateProcessImpl implements BatchMessageProcess<MerchantRiskInfo,MerchantRiskInfoUpdateDto>{

    @Autowired
    private MerchantRiskInfoReportMapper merchantRiskInfoReportMapper;
    @Autowired
    private DataDictInterface dataDictInterface;
    @Override
    public MerchantRiskInfo queryDetail(BatchMessage batchMessage) {
        MerchantRiskInfo merchantRiskInfo=merchantRiskInfoReportMapper.queryById(batchMessage.getSubmitMsgId());
        List<Map<String, Object>> dataAddress = dataDictInterface.getDataAddress("ADDRESS_Select");
        {
            String areaCode=merchantRiskInfo.getOccurarea();
            //String areaCode="210000,220000,210100";
            if(areaCode!=null && !"".equals(areaCode)) {
                String[] stringArr = areaCode.split(",");
                List<AreaNumber> basicData = new ArrayList<>();
                for (int i = 0; i < stringArr.length; i++) {
                    AreaNumber areaNumber = new AreaNumber();
                    for (Map<String, Object> map : dataAddress) {
                        String areaCo = (String) map.get("areaCode");
                        String areaCoSub = areaCo.substring(0, 2);
                        String areaNa = (String) map.get("areaName");
                        if (areaCo.equals(stringArr[i])) {
                            areaNumber.setAreaCode(areaCo);
                            areaNumber.setAreaName(areaNa);
                            areaNumber.setCityName(areaNa);
                            basicData.add(areaNumber);
                        } else {
                            if (stringArr[i].length() > 1) {
                                if (areaCoSub.equals(stringArr[i].substring(0, 2))) {
                                    areaNumber.setAreaCode(areaCo);
                                    areaNumber.setAreaName(areaNa);
                                    List<Map<String, Object>> citys = (List<Map<String, Object>>) map.get("citys");
                                    for (Map<String, Object> mapCity : citys) {
                                        String cityCo = (String) mapCity.get("cityCode");
                                        String cityNa = (String) mapCity.get("cityName");
                                        if (cityCo.equals(stringArr[i])) {
                                            areaNumber.setCityCode(cityCo);
                                            areaNumber.setCityName(areaNa+cityNa);
                                        }
                                    }
                                    basicData.add(areaNumber);
                                }
                            }
                        }
                    }
                }
                merchantRiskInfo.setBasicData(basicData);
            }
        }
        return merchantRiskInfo;
    }
    @Override
    public List query(List<BatchMessage> list) {
        String code=list.get(0).getTrnxCode();
        List<MerchantRiskInfo> List=new ArrayList<>();
        if("UP0004".equals(code)){
         List= merchantRiskInfoReportMapper.queryByListForUpdate(list);
        }else if("UP0004_1".equals(code)){
             List= merchantRiskInfoReportMapper.queryByListForInvalid(list);
        }else if("UP0004_2".equals(code)){
             List= merchantRiskInfoReportMapper.queryByListForDecline(list);
        }
        List<MerchantRiskInfoUpdateDto> bodyList= new ArrayList<>();
        for (MerchantRiskInfo merchantRiskInfo : List) {
            MerchantRiskInfoUpdateDto body= BeanMapperUtil.objConvert(merchantRiskInfo,MerchantRiskInfoUpdateDto.class);
            body.setId(merchantRiskInfo.getRiskId());
            bodyList.add(body);
        }
        log.info("上报数据，bodyList:{}", bodyList);
        return bodyList;
    }


    @Override
    @Transactional
    public void auditFail(List<BatchMessage> batchMessage) {
        for (BatchMessage messageBatchPo : batchMessage) {
            merchantRiskInfoReportMapper.updateStatus(messageBatchPo.getSubmitMsgId(), MessageStatus.REJECT.getCode(),messageBatchPo.getContent());
        }
    }
    @Override
    @Transactional
    public void callback(String status, List<BatchMessage> batchMessage) {
        for (BatchMessage messageBatchPo : batchMessage) {
            merchantRiskInfoReportMapper.updateStatus(messageBatchPo.getSubmitMsgId(),status,messageBatchPo.getContent());
        }
    }
}
